using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BodySoundSimulator.AbsoluteMapData;
using BodySoundSimulator.CaseData;
using BodySoundSimulator.CommonElements;
using System.IO;
using NAudio.Wave;

namespace BodySoundSimulator.Recording
{
	public partial class SoundMapEditor : Form, ISoundMapEditorServices
    {
        #region Drawing related constants
        Pen p = new Pen(Color.Black);
        #endregion

        #region fields
        IApplicationServices appServices;
        CaseInformation caseInfo;
		AuscultationConfiguration configuration;

		TestingAuscultationSitePlayHelper auscultationSoundGeneratorHelper;
		bool suspendUiEvents;
		bool playingStarted;

		SoundMapCellInfo selecetedCellInfo;
        #endregion

        #region Properties
        public CaseInformation CaseInfo
        {
            get { return caseInfo; }
            set { caseInfo = value; }
        }

        #endregion

        #region Constructors
        public SoundMapEditor(IApplicationServices appServices, 
			CaseInformation caseInfo,
			AuscultationConfiguration configuration)
        {
            InitializeComponent();
            this.appServices = appServices;
			this.configuration = configuration;
            this.caseInfo = caseInfo.Clone();
			this.auscultationSoundGeneratorHelper = new TestingAuscultationSitePlayHelper(this.caseInfo, this.configuration, this.View);
			//LoadSiteInfoList();
			this.auscultationSoundGeneratorHelper.LoadSiteInfoList();
        }

        #endregion

        #region private methods

		private void UpdateUiOnPlayStarting()
		{
			this.btnStart.Visible = !this.playingStarted;
			this.btnStop.Visible = this.playingStarted;
			this.radioBackView.Enabled = !this.playingStarted;
			this.radioFrontView.Enabled = !this.playingStarted;
		}

		private void UpdateEditingControl()
		{
			this.pnlSoundMapEditorControl.Controls.Clear();
			if (this.selecetedCellInfo != null)
			{
				switch (this.View)
				{
					case PatientView.FrontView:
						FrontViewSoundMapEffectsEditor frontViewEffectsEditor = new FrontViewSoundMapEffectsEditor(this);
						this.pnlSoundMapEditorControl.Controls.Add(frontViewEffectsEditor);
						break;
					case PatientView.BackView:
						BackViewSoundMapEffectsEditor backViewEffectsEditor = new BackViewSoundMapEffectsEditor(this);
						this.pnlSoundMapEditorControl.Controls.Add(backViewEffectsEditor);
						break;
				}
			}
		}

        #endregion

        #region UI event handlers
        private void picBoxPatientView_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
			g.TranslateTransform(AbsoluteBodyFeatures.OriginTranslateX, AbsoluteBodyFeatures.OriginTranslateY);
            if (this.radioFrontView.Checked)
            {
                //draw front view data
                AbsoluteBodyFeaturesDataManipulator.DrawFrontView(g);
                AuscultationSiteInfoManipultor.DrawFrontViewCaseInfo(g, this.caseInfo);
                SoundMapDataManipulator.DrawFrontViewMap(g, this.caseInfo.SoundMap);
            }
            else
            {
                //draw back view data
                AbsoluteBodyFeaturesDataManipulator.DrawBackView(g);
                AuscultationSiteInfoManipultor.DrawBackViewCaseInfo(g, this.caseInfo);
                SoundMapDataManipulator.DrawBackViewMap(g, this.caseInfo.SoundMap);
            }
			SoundMapDataManipulator.ColorCell(g, this.selecetedCellInfo);
        }      
        
        private void radioPatientView_CheckedChanged(object sender, EventArgs e)
        {
			if (!suspendUiEvents)
			{
				this.selecetedCellInfo = null;
				this.picBoxPatientView.Invalidate();
				this.auscultationSoundGeneratorHelper.View = this.View;
				this.auscultationSoundGeneratorHelper.LoadSiteInfoList();
				UpdateEditingControl();
			}
        }

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
		}

		private void btnStart_Click(object sender, EventArgs e)
		{
			this.auscultationSoundGeneratorHelper.LoadWaveOuts();
			this.playingStarted = true;
			UpdateUiOnPlayStarting();
		}

		private void btnStop_Click(object sender, EventArgs e)
		{
			this.auscultationSoundGeneratorHelper.CloseWaveOuts();
			this.playingStarted = false;
			UpdateUiOnPlayStarting();
		}

		private void btnRefresh_Click(object sender, EventArgs e)
		{

		}

		private void picBoxPatientView_MouseDown(object sender, MouseEventArgs e)
		{
			PointF measureingLocation = new PointF(e.Location.X - AbsoluteBodyFeatures.OriginTranslateX, e.Location.Y - AbsoluteBodyFeatures.OriginTranslateY);
			this.selecetedCellInfo = SoundMapDataManipulator.GetCellInfo(this.caseInfo.SoundMap,
					measureingLocation,
					this.View);
			if (this.playingStarted)
			{
				this.auscultationSoundGeneratorHelper.StartPlaying(this.selecetedCellInfo);
			}
			this.picBoxPatientView.Invalidate();
			UpdateEditingControl();
		}

		private void picBoxPatientView_MouseUp(object sender, MouseEventArgs e)
		{
			//stop playing currently loaded set
			this.auscultationSoundGeneratorHelper.StopPlaying();
		}   		
		
		private void SoundMapEditor_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.auscultationSoundGeneratorHelper.CloseWaveOuts();
		}

		#endregion

		#region ISoundMapEditorServices Members

		public FrontViewDynamicContributionData SelectedFrontViewCellContributionData
		{
			get
			{
				if (this.selecetedCellInfo != null)
				{
					return this.caseInfo.SoundMap.FrontViewContributionMap[this.selecetedCellInfo.CellID];
				}
				return null;
			}
		}

		public BackViewDynamicContributionData SelectedBackViewCellContributionData
		{
			get
			{
				if (this.selecetedCellInfo != null)
				{
					return this.caseInfo.SoundMap.BackViewContributionMap[this.selecetedCellInfo.CellID];
				}
				return null;
			}
		}

		public void UpdateFrontViewEffect(FrontViewCommonAuscultationSiteType type, float value)
		{
			this.caseInfo.SoundMap.FrontViewContributionMap[this.selecetedCellInfo.CellID].FrontViewEffects[(int)type].Effect = value;
		}

		public void UpdateBackViewEffect(BackViewCommonAuscultationSiteType type, float value)
		{
			this.caseInfo.SoundMap.BackViewContributionMap[this.selecetedCellInfo.CellID].BackViewEffects[(int)type].Effect = value;
		}

		public PatientView View
		{
			get
			{
				if (this.radioFrontView.Checked)
				{
					return PatientView.FrontView;
				}
				else
				{
					return PatientView.BackView;
				}
			}
		}
		#endregion
	}
}